//
//  UIView+Category.m
//  dudu_oc_master
//
//  Created by mac on 2019/11/11.
//  Copyright © 2019 Shawn Ma. All rights reserved.
//

#import "UIView+Category.h"

@implementation UIView (Category)


- (void)setTj_x:(CGFloat)tj_x {
    CGRect frame = self.frame;
    frame.origin.x = tj_x;
    self.frame = frame;
    
}

- (CGFloat)tj_x {
    return self.frame.origin.x;
}

- (void)setTj_y:(CGFloat)tj_y {
    CGRect frame = self.frame;
    frame.origin.y = tj_y;
    self.frame = frame;
}

- (CGFloat)tj_y {
    return self.frame.origin.y;
}

- (void)setTj_width:(CGFloat)tj_width {
    CGRect frame = self.frame;
    frame.size.width = tj_width;
    self.frame = frame;
}

- (CGFloat)tj_width {
    return self.frame.size.width;
}

- (void)setTj_height:(CGFloat)tj_height {
    CGRect frame = self.frame;
    frame.size.height = tj_height;
    self.frame = frame;
}

- (CGFloat)tj_height {
    return self.frame.size.height;
}

- (void)setTj_size:(CGSize)tj_size {
    CGRect frame = self.frame;
    frame.size = tj_size;
    self.frame = frame;
}

- (CGSize)tj_size {
    return self.frame.size;
}

- (void)setTj_centerX:(CGFloat)tj_centerX {
    CGPoint center = self.center;
    center.x = tj_centerX;
    self.center = center;
}

- (CGFloat)tj_centerX {
    return self.center.x;
}

- (void)setTj_centerY:(CGFloat)tj_centerY {
    CGPoint center = self.center;
    center.y = tj_centerY;
    self.center = center;
}


- (CGFloat)tj_top {
    return self.frame.origin.y;
}

- (void)setTj_top:(CGFloat)tj_top {
    CGRect frame = self.frame;
    frame.origin.y = tj_top;
    self.frame = frame;
}

- (void)setTj_bottom:(CGFloat)tj_bottom {
    CGRect frame = self.frame;
    frame.origin.y = tj_bottom - frame.size.height;
    self.frame = frame;
}


- (CGFloat)tj_bottom {
    return self.frame.origin.y + self.frame.size.height;
}
 
- (CGFloat)tj_centerY {
    return self.center.y;
}

- (CGFloat)tj_MaxY {
    return CGRectGetMaxY(self.frame);
}

- (CGFloat)tj_MaxX {
    return CGRectGetMaxX(self.frame);
}


+ (_Nonnull instancetype)addToView:(nonnull UIView *)superView {
    UIView *view = [[self alloc] init];
    [superView addSubview:view];
    return view;
}

+ (_Nonnull instancetype)buttonWithSuperView:(UIView *)superView font:(UIFont *)font color:(nullable UIColor *)color title:(nullable NSString *)title {
    UIButton *button = (UIButton *)[[self alloc] init];
    if (superView) [superView addSubview:button];
    [button setTitle:title forState:UIControlStateNormal];
    if (color) [button setTitleColor:color forState:UIControlStateNormal];
    button.titleLabel.font = font;
    return button;
}

+ (_Nonnull instancetype)lableWithSuperView:(UIView *)superView font:(UIFont *)font color:(nullable UIColor *)color title:(nullable NSString *)title textAlignment:(NSTextAlignment)textAlignment {
    UILabel *label = (UILabel *)[[self alloc] init];
    if (superView) [superView addSubview:label];
    label.text = title;
    if (color) label.textColor = color;
    label.font = font;
    label.textAlignment = textAlignment;
    return label;
}

- (UITapGestureRecognizer *)addTapGesturesWithTarget:(nullable id)id action:(nonnull SEL)action {
    self.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:id action:action];
    [self addGestureRecognizer:tapGestureRecognizer];
    return tapGestureRecognizer;
}

- (UILongPressGestureRecognizer *)addLongPressGesturesWithTarget:(id)id action:(SEL)action {
    self.userInteractionEnabled = YES;
    UILongPressGestureRecognizer *longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:id action:action];

    [self addGestureRecognizer:longPressGestureRecognizer];
    return longPressGestureRecognizer;
}

- (void)addRoundingCorners:(UIRectCorner)corners radius:(CGFloat)radius {
    UIBezierPath *path = nil;
    
    if (radius > 0) {
        path = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:corners cornerRadii:CGSizeMake(radius, radius)];
    } else {
        path = [UIBezierPath bezierPathWithRect:self.bounds];
    }
    
    CAShapeLayer *shape = [[CAShapeLayer alloc] init];
    shape.frame = self.bounds;
    [shape setPath:path.CGPath];
    self.layer.mask = shape;
}

- (CAShapeLayer *)shapeLayerRoundingCorners:(UIRectCorner)corners radius:(CGFloat)radius {
    UIBezierPath *path = nil;
    if (radius > 0) {
        path = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:corners cornerRadii:CGSizeMake(radius, radius)];
    } else {
        path = [UIBezierPath bezierPathWithRect:self.bounds];
    }
    CAShapeLayer *shape = [[CAShapeLayer alloc] init];
    shape.frame = self.bounds;
    [shape setPath:path.CGPath];
    return shape;
}

- (CAShapeLayer *)shapeLayerRoundingCorners:(UIRectCorner)corners rect:(CGRect)rect radius:(CGFloat)radius {
    UIBezierPath *path = nil;
    if (radius > 0) {
        path = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:corners cornerRadii:CGSizeMake(radius, radius)];
    } else {
        path = [UIBezierPath bezierPathWithRect:rect];
    }
    CAShapeLayer *shape = [[CAShapeLayer alloc] init];
    shape.frame = self.bounds;
    [shape setPath:path.CGPath];
    return shape;
}

- (instancetype (^)(UIView *superView))addTo {
    return ^id (UIView *superView){
        [superView addSubview:self];
        return self;
    };
}

- (instancetype (^)(CGFloat radius))addCornerRadius {
    return ^id (CGFloat radius){
        self.layer.cornerRadius = radius;
        self.layer.masksToBounds = YES;
        return self;
    };
}


- (UIViewController*)viewController {
    for (UIView* next = [self superview]; next; next = next.superview) {
        UIResponder* nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            return (UIViewController*)nextResponder;
        }
    }
    return nil;
}

- (void)removeAllSubview {
    [self.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj removeFromSuperview];
    }];
}

- (UIColor *)colorOfPoint:(CGPoint)point {
    unsigned char pixel[4] = {0};
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = CGBitmapContextCreate(pixel, 1, 1, 8, 4, colorSpace, (CGBitmapInfo)kCGImageAlphaPremultipliedLast);

    CGContextTranslateCTM(context, -point.x, -point.y);
    [self.layer renderInContext:context];

    CGContextRelease(context);
    CGColorSpaceRelease(colorSpace);

    UIColor *color = [UIColor colorWithRed:pixel[0]/255.0 green:pixel[1]/255.0 blue:pixel[2]/255.0 alpha:pixel[3]/255.0];
    return color;
}

- (UIView *)addLineViewWithColor:(UIColor *)color block:(void(^)(MASConstraintMaker *make))block {
    UIView *lineView = [UIView addToView:self];
    [lineView mas_makeConstraints:block];
    lineView.backgroundColor = color;
    return lineView;
}
 
@end
